<?php 

use Fusonic\SpreadsheetExport\Spreadsheet;
use Fusonic\SpreadsheetExport\ColumnTypes\CurrencyColumn;
use Fusonic\SpreadsheetExport\ColumnTypes\DateColumn;
use Fusonic\SpreadsheetExport\ColumnTypes\NumericColumn;
use Fusonic\SpreadsheetExport\ColumnTypes\TextColumn;
use Fusonic\SpreadsheetExport\Writers\CsvWriter;
use Fusonic\SpreadsheetExport\Writers\TsvWriter;
use Fusonic\SpreadsheetExport\Writers\OdsWriter;

/**
 * @file Script que contém as funções para geração do calendário
 */

/** funcao que cria o calendario e faz o download */
function frequencia_relatorio_alunos() {
	$export = new Spreadsheet();
	
	// colunas
	$export->AddColumn(new TextColumn("Nome", 5));
	$export->AddColumn(new TextColumn("CPF", 3));
	$export->AddColumn(new TextColumn("Banco", 2));
	$export->AddColumn(new TextColumn("Agência", 3));
	$export->AddColumn(new TextColumn("Conta", 3));
	$export->AddColumn(new TextColumn("Laboratório", 3));
	$export->AddColumn(new TextColumn("Coordenador", 5));
	$export->AddColumn(new TextColumn("Turno", 2));
	
	$query = db_query("SELECT DISTINCT 
			a.nome, a.cpf, b.nome AS banco, a.agencia, a.conta, l.nome AS laboratorio, u.name AS coordenador, t.turno
			FROM coordenador_aluno AS c
			INNER JOIN aluno AS a
			INNER JOIN banco AS b
			INNER JOIN laboratorio AS l
			INNER JOIN users AS u
			INNER JOIN lotacao AS t
			WHERE c.aluno_id = a.id 
			AND c.coordenador_id = u.uid
			AND a.banco_id = b.id
			AND t.laboratorio_id = l.id
			AND t.aluno_id = a.id;");
	
	$turno = array(0 => "Manhã", 1 => "Tarde", 2 => "Noite");
	foreach ($query as $row) {
		$export->AddRow( array(	$row->nome, $row->cpf, $row->banco, 
														$row->agencia, $row->conta, $row->laboratorio, 
														$row->coordenador, $turno[$row->turno]) );
	}
	
	$writer = new OdsWriter();
	$writer->includeColumnHeaders = true;
	
	$export->download($writer, "RelatorioBolsistasLotados_" . date("d-m-Y"));
}